from functools import wraps
from fastapi import Request
from ..repositories.log_repository import add_log


def log_required(func):
    """
    装饰器：添加日志
    :return: True/False
    """
    @wraps(func)
    async def wrapper(request: Request, *args, **kwargs):
        user_id = request.state.user_id
        content = f'path:"{request.url.path}", query:"{dict(request.query_params)}"'
        await add_log(user_id, content)
        return await func(request, *args, **kwargs)
    return wrapper
